//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "HybridHestonHullWhiteProcess.h"
using namespace Cephei::QL::Processes;
#include <gen/QL/Processes/HestonProcess.h>
#include <gen/QL/Processes/HullWhiteForwardProcess.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/StochasticProcess.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Processes::CHybridHestonHullWhiteProcess::CHybridHestonHullWhiteProcess (Cephei::QL::Processes::IHestonProcess^ hestonProcess, Cephei::QL::Processes::IHullWhiteForwardProcess^ hullWhiteProcess, Double corrEquityShortRate, Microsoft::FSharp::Core::FSharpOption<QL::Processes::HybridHestonHullWhiteProcess::DiscretizationEnum>^ discretization) : CStochasticProcess(CHybridHestonHullWhiteProcess::typeid)
{
    CHestonProcess^ _ChestonProcess;
    CHullWhiteForwardProcess^ _ChullWhiteProcess;
    try
    {
#ifdef HANDLE
        _phHybridHestonHullWhiteProcess = NULL;
#endif
        _ChestonProcess = safe_cast<CHestonProcess^> (hestonProcess);
        _ChestonProcess->Lock();
        boost::shared_ptr<QuantLib::HestonProcess>& _hestonProcess = static_cast<boost::shared_ptr<QuantLib::HestonProcess>&> (_ChestonProcess->GetShared ()); 
        _ChullWhiteProcess = safe_cast<CHullWhiteForwardProcess^> (hullWhiteProcess);
        _ChullWhiteProcess->Lock();
        boost::shared_ptr<QuantLib::HullWhiteForwardProcess>& _hullWhiteProcess = static_cast<boost::shared_ptr<QuantLib::HullWhiteForwardProcess>&> (_ChullWhiteProcess->GetShared ()); 
        QuantLib::Real _corrEquityShortRate = (QuantLib::Real)ValueHelper::Convert (corrEquityShortRate); //d
        QuantLib::HybridHestonHullWhiteProcess::Discretization _discretization = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Processes::HybridHestonHullWhiteProcess::DiscretizationEnum>::IsSome::get (discretization) ? (QuantLib::HybridHestonHullWhiteProcess::Discretization)discretization->Value : QuantLib::HybridHestonHullWhiteProcess::Discretization::BSMHullWhite); //10
        _ppHybridHestonHullWhiteProcess = new boost::shared_ptr<QuantLib::HybridHestonHullWhiteProcess> (new QuantLib::HybridHestonHullWhiteProcess ( _hestonProcess,  _hullWhiteProcess,  _corrEquityShortRate,  _discretization ));
        SetStochasticProcess (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppHybridHestonHullWhiteProcess));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_ChestonProcess != nullptr) _ChestonProcess->Unlock();
        if (_ChullWhiteProcess != nullptr) _ChullWhiteProcess->Unlock();
    }
}
Cephei::QL::Processes::CHybridHestonHullWhiteProcess::CHybridHestonHullWhiteProcess (boost::shared_ptr<QuantLib::HybridHestonHullWhiteProcess>& childNative, Object^ owner) : CStochasticProcess(CHybridHestonHullWhiteProcess::typeid)
{
#ifdef HANDLE
	_phHybridHestonHullWhiteProcess = NULL;
#endif
	_ppHybridHestonHullWhiteProcess = &childNative;
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppHybridHestonHullWhiteProcess));
}
Cephei::QL::Processes::CHybridHestonHullWhiteProcess::CHybridHestonHullWhiteProcess (QuantLib::HybridHestonHullWhiteProcess& childNative, Object^ owner) : CStochasticProcess(CHybridHestonHullWhiteProcess::typeid)
{
#ifdef HANDLE
	_phHybridHestonHullWhiteProcess = NULL;
#endif
	_ppHybridHestonHullWhiteProcess = new boost::shared_ptr<QuantLib::HybridHestonHullWhiteProcess> (&childNative);
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppHybridHestonHullWhiteProcess));
    _HybridHestonHullWhiteProcessOwner = owner;
    _StochasticProcessOwner = owner;
}

Cephei::QL::Processes::CHybridHestonHullWhiteProcess::CHybridHestonHullWhiteProcess (CHybridHestonHullWhiteProcess^ copy) : CStochasticProcess(CHybridHestonHullWhiteProcess::typeid)
{
#ifdef HANDLE
	_phHybridHestonHullWhiteProcess = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppHybridHestonHullWhiteProcess = new boost::shared_ptr<QuantLib::HybridHestonHullWhiteProcess> (copy->GetShared());
        _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppHybridHestonHullWhiteProcess));
    }
}
Cephei::QL::Processes::CHybridHestonHullWhiteProcess::CHybridHestonHullWhiteProcess (PLATFORM::Type^ t) : CStochasticProcess(CHybridHestonHullWhiteProcess::typeid)
{
#ifdef HANDLE
	_phHybridHestonHullWhiteProcess = NULL;
#endif
	if (!t->IsSubclassOf(CHybridHestonHullWhiteProcess::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Processes::CHybridHestonHullWhiteProcess::CHybridHestonHullWhiteProcess (QuantLib::Handle<QuantLib::HybridHestonHullWhiteProcess>& childNative, Object^ owner)  : CStochasticProcess(CHybridHestonHullWhiteProcess::typeid)
{
	_phHybridHestonHullWhiteProcess = &childNative;
	_ppHybridHestonHullWhiteProcess = &static_cast<boost::shared_ptr<QuantLib::HybridHestonHullWhiteProcess>>(childNative.currentLink());
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppHybridHestonHullWhiteProcess));
    _HybridHestonHullWhiteProcessOwner = owner;
}
Cephei::QL::Processes::CHybridHestonHullWhiteProcess::CHybridHestonHullWhiteProcess (QuantLib::Handle<QuantLib::HybridHestonHullWhiteProcess> childNative)  : CStochasticProcess(CHybridHestonHullWhiteProcess::typeid)
{
	_phHybridHestonHullWhiteProcess = &childNative;
	_ppHybridHestonHullWhiteProcess = &static_cast<boost::shared_ptr<QuantLib::HybridHestonHullWhiteProcess>>(childNative.currentLink());
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppHybridHestonHullWhiteProcess));
}
#endif
#ifdef STRUCT
Cephei::QL::Processes::CHybridHestonHullWhiteProcess::CHybridHestonHullWhiteProcess (QuantLib::HybridHestonHullWhiteProcess childNative)  : CStochasticProcess(CHybridHestonHullWhiteProcess::typeid)
{
#ifdef HANDLE
	_phHybridHestonHullWhiteProcess = NULL;
#endif
	_ppHybridHestonHullWhiteProcess = new boost::shared_ptr<QuantLib::HybridHestonHullWhiteProcess> (new QuantLib::HybridHestonHullWhiteProcess (childNative));
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppHybridHestonHullWhiteProcess));
}
#endif

Cephei::QL::Processes::CHybridHestonHullWhiteProcess::~CHybridHestonHullWhiteProcess ()
{
    if (_ppHybridHestonHullWhiteProcess != NULL)
    {
	    delete _ppHybridHestonHullWhiteProcess;
        _ppHybridHestonHullWhiteProcess = NULL;
    }
}
Cephei::QL::Processes::CHybridHestonHullWhiteProcess::!CHybridHestonHullWhiteProcess ()
{
    if (_ppHybridHestonHullWhiteProcess != NULL)
    {
	    delete _ppHybridHestonHullWhiteProcess;
    }
}
QuantLib::HybridHestonHullWhiteProcess& Cephei::QL::Processes::CHybridHestonHullWhiteProcess::GetReference ()
{
    if (_ppHybridHestonHullWhiteProcess == NULL) throw REFNEW NativeNullException ();
	return **_ppHybridHestonHullWhiteProcess;
}
boost::shared_ptr<QuantLib::HybridHestonHullWhiteProcess>& Cephei::QL::Processes::CHybridHestonHullWhiteProcess::GetShared ()
{
    if (_ppHybridHestonHullWhiteProcess == NULL) throw REFNEW NativeNullException ();
	return *_ppHybridHestonHullWhiteProcess;
}
QuantLib::HybridHestonHullWhiteProcess* Cephei::QL::Processes::CHybridHestonHullWhiteProcess::GetPointer ()
{
    if (_ppHybridHestonHullWhiteProcess == NULL) throw REFNEW NativeNullException ();
	return &**_ppHybridHestonHullWhiteProcess;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::HybridHestonHullWhiteProcess>& Cephei::QL::Processes::CHybridHestonHullWhiteProcess::GetHandle ()
{
	if (_phHybridHestonHullWhiteProcess == NULL)
	{
		_phHybridHestonHullWhiteProcess = new Handle<QuantLib::HybridHestonHullWhiteProcess> (*_ppHybridHestonHullWhiteProcess);
	}
	return *_phHybridHestonHullWhiteProcess;
}
#endif
bool Cephei::QL::Processes::CHybridHestonHullWhiteProcess::HasNative () 
{
	return (_ppHybridHestonHullWhiteProcess != NULL);
}

Cephei::QL::Processes::IHestonProcess^ Cephei::QL::Processes::CHybridHestonHullWhiteProcess::HestonProcess::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::HestonProcess>& _rv = (boost::shared_ptr<QuantLib::HestonProcess>&)(*_ppHybridHestonHullWhiteProcess)->hestonProcess ( );   
        Cephei::QL::Processes::CHestonProcess^ _nrv = REFNEW Cephei::QL::Processes::CHestonProcess (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Processes::IHullWhiteForwardProcess^ Cephei::QL::Processes::CHybridHestonHullWhiteProcess::HullWhiteProcess::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::HullWhiteForwardProcess>& _rv = (boost::shared_ptr<QuantLib::HullWhiteForwardProcess>&)(*_ppHybridHestonHullWhiteProcess)->hullWhiteProcess ( );   
        Cephei::QL::Processes::CHullWhiteForwardProcess^ _nrv = REFNEW Cephei::QL::Processes::CHullWhiteForwardProcess (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHybridHestonHullWhiteProcess::Numeraire (Double t, Cephei::QL::Math::IArray^ x)
{
    CArray^ _Cx;
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        _Cx = safe_cast<CArray^> (x);
        _Cx->Lock();
        QuantLib::Array& _x = static_cast<QuantLib::Array&> (_Cx->GetReference ()); 
    	QuantLib::DiscountFactor _rv = (QuantLib::DiscountFactor)(*_ppHybridHestonHullWhiteProcess)->numeraire ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
Cephei::QL::Processes::IHybridHestonHullWhiteProcess^ Cephei::QL::Processes::CHybridHestonHullWhiteProcess::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppHybridHestonHullWhiteProcess)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Processes::HybridHestonHullWhiteProcess::DiscretizationEnum Cephei::QL::Processes::CHybridHestonHullWhiteProcess::Discretization::get ()
{
    try
    {
    	QuantLib::HybridHestonHullWhiteProcess::Discretization _rv = (QuantLib::HybridHestonHullWhiteProcess::Discretization)(*_ppHybridHestonHullWhiteProcess)->discretization ( );   
        QL::Processes::HybridHestonHullWhiteProcess::DiscretizationEnum _nrv = (QL::Processes::HybridHestonHullWhiteProcess::DiscretizationEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHybridHestonHullWhiteProcess::Eta::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHybridHestonHullWhiteProcess)->eta ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Processes::CHybridHestonHullWhiteProcess::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppHybridHestonHullWhiteProcess)->size ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHybridHestonHullWhiteProcess::Time (DateTime date)
{
    try
    {
        QuantLib::Date _date = (QuantLib::Date)ValueHelper::Convert (date); //a
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppHybridHestonHullWhiteProcess)->time ( _date );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Processes::IHybridHestonHullWhiteProcess^ Cephei::QL::Processes::CHybridHestonHullWhiteProcess_Factory::Create (Cephei::QL::Processes::IHestonProcess^ hestonProcess, Cephei::QL::Processes::IHullWhiteForwardProcess^ hullWhiteProcess, Double corrEquityShortRate, Microsoft::FSharp::Core::FSharpOption<QL::Processes::HybridHestonHullWhiteProcess::DiscretizationEnum>^ discretization)
{
    return REFNEW CHybridHestonHullWhiteProcess ( hestonProcess,  hullWhiteProcess,  corrEquityShortRate,  discretization);
}
